<?php

namespace App\Http\Middleware;

use Closure;
use Sentinel;

class RedirectIfAuthenticated
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Sentinel::check() || $request->is('*/login')){
            return $next($request);
        }
        if($request->ajax()){
            //用户已登出默认异常提示信息
            $defaultSet = config('exceptions.user.logout');
            return formatJsonResponse(null, $defaultSet['message'], $defaultSet['errCode']);
        }else{
            return redirect()->guest('admin/user/login');
        }

        return $next($request);
    }
}
